<template>
  <el-dialog :before-close="handleClose" :visible="showJop" :title="title" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="排序" prop="jobSort">
        <el-input-number v-model.number="form.jobSort" :min="0" :max="999" controls-position="right" style="width: 370px;" />
      </el-form-item>
      <el-form-item v-if="form.pid !== 0" label="状态" prop="enabled">
        <el-radio v-for="item in jobStatus" :key="item.id" v-model="form.enabled" :label="item.value === 'true'">
          {{ item.label }}
        </el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="submitJop">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { add, edit } from '@/api/system/job'
export default {
  props: {
    showJop: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    editData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      jobStatus: [
        { id: 1, value: 'true', label: '启用' },
        { id: 2, value: 'false', label: '禁用' }
      ],
      form: {
        enabled: true,
        name: '',
        jobSort: 999
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        jobSort: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ]
      }
    }
  },
  watch: {
    showJop() {
      if (this.showJop) this.initForm()
    }
  },
  methods: {
    initForm() {
      let pamrms = {
        enabled: true,
        jobSort: 999,
        name: ''
      }
      if (this.title === '编辑岗位') {
        pamrms = Object.assign(this.form, this.editData)
      }
      this.form = pamrms
    },
    // 关闭弹框
    handleClose() {
      this.form.enabled = true
      this.form.jobSort = 999
      this.form.name = ''
      this.$emit('update:showJop', false)
    },
    submitJop() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '新增岗位') {
            this.addJob()
          } else this.editJob(this.form)
        } else {
          return false
        }
      })
    },
    addJob() {
      add({ ...this.form }).then(res => {
        this.handleClose()
        this.$message.success('操作成功~')
        this.$emit('search')
      })
    },
    editJob(v) {
      edit({ ...v }).then(res => {
        this.handleClose()
        this.$message.success('操作成功~')
        this.$emit('search')
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
  /deep/.el-form-item--small.el-form-item{
    margin-bottom: 25px;
  }
</style>
